<template><div><h2 id="资源-assets" tabindex="-1"><a class="header-anchor" href="#资源-assets"><span>资源（Assets）</span></a></h2>
<p>Yii 中的资源是和 Web 页面相关的文件，可为 CSS，JS 文件，图片或视频等，资源放在 Web 可访问的目录下，直接被 Web 服务器调用。</p>
<p>通过程序自动管理资源更好一点，例如，当你在页面中使用 <code v-pre>yii\jui\DatePicker</code> 小部件时，它会自动包含需要的 CSS 和 JS 文件，而不是要求你手工去找到这些文件并包含，当你升级小部件时，它会自动使用新版本的资源文件，在本教程中，我们会详述 Yii 提供的强大的资源管理功能。</p>
<h2 id="资源包" tabindex="-1"><a class="header-anchor" href="#资源包"><span>资源包</span></a></h2>
<p>Yii 在 <em>资源包</em> 中管理资源，资源包简单的说就是放在一个目录下的资源集合，当在 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-views.md" target="_blank" rel="noopener noreferrer">视图</a> 中注册一个资源包，在渲染 Web 页面时会包含包中的 CSS 和 JS 文件。</p>
<h2 id="定义资源包" tabindex="-1"><a class="header-anchor" href="#定义资源包"><span>定义资源包</span></a></h2>
<p>资源包指定为继承 <code v-pre>yii\web\AssetBundle</code> 的 PHP 类，包名为可 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-autoloading.md" target="_blank" rel="noopener noreferrer">自动加载</a> 的 PHP 类名，在资源包类中，要指定资源所在位置，包含哪些 css 和 js 文件以及和其他包的依赖关系。</p>
<p>如下代码定义 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/start-installation.md" target="_blank" rel="noopener noreferrer">基础应用模板</a> 使用的主要资源包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">app<span class="token punctuation">\</span>assets</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>AssetBundle</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">AppAsset</span> <span class="token keyword">extends</span> <span class="token class-name">AssetBundle</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$basePath</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'@webroot'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$baseUrl</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'@web'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$css</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'css/site.css'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'css/print.css'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'media'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'print'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$js</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$depends</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'yii\web\YiiAsset'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'yii\bootstrap\BootstrapAsset'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如上 <code v-pre>AppAsset</code> 类指定资源文件放在 <code v-pre>@webroot</code> 目录下，对应的 URL 为 <code v-pre>@web</code>，资源包中包含一个 CSS 文件 <code v-pre>css/site.css</code>，没有 JS 文件，依赖其他两个包 <code v-pre>yii\web\YiiAsset</code> 和 <code v-pre>yii\bootstrap\BootstrapAsset</code>，关于 <code v-pre>yii\web\AssetBundle</code> 的属性的更多详细如下所述：</p>
<ul>
<li>[[yii\web\AssetBundle::sourcePath]]：指定包包含资源文件的根目录，当根目录不能被 Web 访问时该属性应设置，否则，应设置 [[yii\web\AssetBundle::basePath]] 属性和 [[yii\web\AssetBundle::baseUrl]]。<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-aliases.md" target="_blank" rel="noopener noreferrer">路径别名</a> 可在此处使用；</li>
<li>[[yii\web\AssetBundle::basePath]]：指定包含资源包中资源文件并可 Web 访问的目录，当指定 [[yii\web\AssetBundle::sourcePath]] 属性，<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-assets.md#asset-manager" target="_blank" rel="noopener noreferrer">资源管理器</a> 会发布包的资源到一个可 Web 访问并覆盖该属性，如果你的资源文件在一个 Web 可访问目录下，应设置该属性，这样就不用再发布了。<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-aliases.md" target="_blank" rel="noopener noreferrer">路径别名</a> 可在此处使用。</li>
<li>[[yii\web\AssetBundle::baseUrl]]：指定对应到 [[yii\web\AssetBundle::basePath]] 目录的 URL，和 [[yii\web\AssetBundle::basePath]] 类似，如果你指定 [[yii\web\AssetBundle::sourcePath]] 属性，<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-assets.md#asset-manager" target="_blank" rel="noopener noreferrer">资源管理器</a> 会发布这些资源并覆盖该属性，<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-aliases.md" target="_blank" rel="noopener noreferrer">路径别名</a> 可在此处使用。</li>
<li>[[yii\web\AssetBundle::css]]：列出此包中包含的 CSS 文件的数组。请注意，只应使用正斜杠 <code v-pre>/</code> 作为目录分隔符。每个文件都可以单独指定为字符串，也可以与属性标记及其值一起指定在数组中。</li>
<li>[[yii\web\AssetBundle::js]]：列出此包中包含的 JS 文件的数组。请注意，只应使用正斜杠 <code v-pre>/</code> 作为目录分隔符。每个 JS 文件可指定为以下两种格式之一：
<ul>
<li>相对路径表示为本地 JavaScript 文件 (如 <code v-pre>js/main.js</code>)，文件实际的路径在该相对路径前加上 [[yii\web\AssetManager::basePath]]，文件实际的 URL 在该路径前加上 [[yii\web\AssetManager::baseUrl]]。</li>
<li>绝对 URL 地址表示为外部 JS 文件，如 <code v-pre>http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js</code> 或 <code v-pre>//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js</code>。</li>
</ul>
</li>
<li>[[yii\web\AssetBundle::depends]]：一个列出该资源包依赖的其他资源包（后两节有详细介绍）。</li>
<li>[[yii\web\AssetBundle::jsOptions]]：当调用 [[yii\web\View::registerJsFile()]] 注册该包 <em>每个</em> JS 文件时，指定传递到该方法的选项。</li>
<li>[[yii\web\AssetBundle::cssOptions]]：当调用 [[yii\web\View::registerCssFile()]] 注册该包 <em>每个</em> CSS 文件时，指定传递到该方法的选项。</li>
<li>[[yii\web\AssetBundle::publishOptions]]：当调用 [[yii\web\AssetManager::publish()]] 发布该包资源文件到 Web 目录时，指定传递到该方法的选项，仅在指定了 [[yii\web\AssetBundle::sourcePath]] 属性时使用。</li>
</ul>
<h3 id="资源位置" tabindex="-1"><a class="header-anchor" href="#资源位置"><span>资源位置</span></a></h3>
<p>资源根据它们的位置可以分为：</p>
<ul>
<li>源资源: 资源文件和 PHP 源代码放在一起，不能被 Web 直接访问，为了使用这些源资源，它们要拷贝到一个可 Web 访问的 Web 目录中 成为发布的资源，这个过程称为 <em>发布资源</em>，随后会详细介绍。</li>
<li>发布资源: 资源文件放在可通过 Web 直接访问的 Web 目录中；</li>
<li>外部资源: 资源文件放在与你的 Web 应用不同的 Web 服务器上；</li>
</ul>
<p>当定义资源包类时候，如果你指定了[[yii\web\AssetBundle::sourcePath]] 属性，就表示任何使用相对路径的资源会被当作源资源；如果没有指定该属性，就表示这些资源为发布资源（因此应指定[[yii\web\AssetBundle::basePath]] 和 [[yii\web\AssetBundle::baseUrl]] 让 Yii 知道它们的位置）。</p>
<p>推荐将资源文件放到 Web 目录以避免不必要的发布资源过程，这就是之前的例子：指定 [[yii\web\AssetBundle::basePath]] 而不是 [[yii\web\AssetBundle::sourcePath]].</p>
<p>对于 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-extensions.md" target="_blank" rel="noopener noreferrer">扩展</a> 来说，由于它们的资源和源代码都在不能 Web 访问的目录下， 在定义资源包类时必须指定 [[yii\web\AssetBundle::sourcePath]] 属性。</p>
<blockquote>
<p>Note: [[yii\web\AssetBundle::sourcePath]] 属性不要用 <code v-pre>@webroot/assets</code>，该路径默认为 [[yii\web\AssetManager]] 资源管理器将源资源发布后存储资源的路径，该路径的所有内容会认为是临时文件，可能会被删除。</p>
</blockquote>
<h3 id="资源依赖" tabindex="-1"><a class="header-anchor" href="#资源依赖"><span>资源依赖</span></a></h3>
<p>当 Web 页面包含多个 CSS 或 JS 文件时，它们有一定的先后顺序以避免属性覆盖，例如，Web 页面在使用 jQuery UI 小部件前必须确保 jQuery JS 文件已经被包含了，我们称这种资源先后次序称为资源依赖。</p>
<p>资源依赖主要通过 [[yii\web\AssetBundle::depends]] 属性来指定，在 <code v-pre>AppAsset</code> 示例中，资源包依赖其他两个资源包：<code v-pre>yii\web\YiiAsset</code> 和 <code v-pre>yii\bootstrap\BootstrapAsset</code> 也就是该资源包的 CSS 和 JS 文件要在这两个依赖包的文件包含 <em>之后</em> 才包含。</p>
<p>资源依赖关系是可传递，也就是说 A 依赖 B，B 依赖 C，那么 A 也依赖 C。</p>
<h3 id="资源选项" tabindex="-1"><a class="header-anchor" href="#资源选项"><span>资源选项</span></a></h3>
<p>可指定 [[yii\web\AssetBundle::cssOptions]] 和 [[yii\web\AssetBundle::jsOptions]] 属性来自定义页面包含 CSS 和 JS 文件的方式，这些属性值会分别传递给 [[yii\web\View::registerCssFile()]] 和 [[yii\web\View::registerJsFile()]] 方法， 在 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-views.md" target="_blank" rel="noopener noreferrer">视图</a> 调用这些方法包含 CSS 和 JS 文件时。</p>
<blockquote>
<p>Note: 在资源包类中设置的选项会应用到该包中 <em>每个</em> CSS 或 JS 文件，如果想对每个文件使用不同的选项，应创建不同的资源包并在每个包中使用一个选项集。</p>
</blockquote>
<p>例如，只想 IE9 或更高的浏览器包含一个 CSS 文件，可以使用如下选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">public</span> <span class="token variable">$cssOptions</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'condition'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'lte IE9'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这会使包中的 CSS 文件使用以下 HTML 标签包含进来：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span><span class="token punctuation">[</span><span class="token keyword">if</span> lte <span class="token constant">IE9</span><span class="token punctuation">]</span><span class="token operator">></span></span>
<span class="line"><span class="token operator">&lt;</span>link rel<span class="token operator">=</span><span class="token string double-quoted-string">"stylesheet"</span> href<span class="token operator">=</span><span class="token string double-quoted-string">"path/to/foo.css"</span><span class="token operator">></span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token punctuation">[</span><span class="token keyword">endif</span><span class="token punctuation">]</span><span class="token operator">--</span><span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>为链接标签包含 <code v-pre>&lt;noscript&gt;</code> 可使用如下代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">public</span> <span class="token variable">$cssOptions</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'noscript'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>为使 JavaScript 文件包含在页面 head 区域（JavaScript 文件默认包含在 body 的结束处）使用以下选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">public</span> <span class="token variable">$jsOptions</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'position'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>View</span><span class="token operator">::</span><span class="token constant">POS_HEAD</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>默认情况下，当发布资源包时，所有在 [[yii\web\AssetBundle::sourcePath]] 目录里的内容都会发布。你可以通过配置 [[yii\web\AssetBundle::publishOptions]] 属性来自定义这种行为。 比如，为了只发布 [[yii\web\AssetBundle::sourcePath]] 其中的某些内容或子目录里的内容， 可以在资源类中试试下面的做法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">app<span class="token punctuation">\</span>assets</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>AssetBundle</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">FontAwesomeAsset</span> <span class="token keyword">extends</span> <span class="token class-name">AssetBundle</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$sourcePath</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'@bower/font-awesome'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$css</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'css/font-awesome.min.css'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$publishOptions</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'only'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'fonts/'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'css/'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>上述的代码为 <a href="https://fontawesome.com/" target="_blank" rel="noopener noreferrer">“fontawesome” package</a> 定义了资源包。通过配置发布选项的 only 下标，只有 <code v-pre>fonts</code> 和 <code v-pre>css</code> 子目录会发布。</p>
<h3 id="bower-和-npm-资源安装" tabindex="-1"><a class="header-anchor" href="#bower-和-npm-资源安装"><span>Bower 和 NPM 资源安装</span></a></h3>
<p>大多数 JS 或 CSS 包使用 <a href="http://bower.io/" target="_blank" rel="noopener noreferrer">Bower</a> 或 <a href="https://www.npmjs.com/" target="_blank" rel="noopener noreferrer">NPM</a> 来管理。在 PHP 中，我们用 Composer 来管理 PHP 依赖。像 PHP 包一样，也可以使用 <code v-pre>composer.json</code> 管理 Bower 和 NPM 包。</p>
<p>要实现这一点，需要配置一下 Composer 。有下面两种方法：</p>
<h4 id="使用-asset-packagist-库" tabindex="-1"><a class="header-anchor" href="#使用-asset-packagist-库"><span>使用 asset-packagist 库</span></a></h4>
<p>这种方式将满足大多数需要使用 NPM 或 Bower 包项目的要求。</p>
<blockquote>
<p>Note: 从 2.0.13 开始，基本和高级应用程序模板都默认配置使用 asset-packagist ， 因此你可以跳过本节。</p>
</blockquote>
<p>在你项目 <code v-pre>composer.json</code> 文件中，添加下面几行代码：</p>
<div class="language-javascript line-numbers-mode" data-highlighter="prismjs" data-ext="js" data-title="js"><pre v-pre class="language-javascript"><code><span class="line"><span class="token string-property property">"repositories"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token string-property property">"type"</span><span class="token operator">:</span> <span class="token string">"composer"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string-property property">"url"</span><span class="token operator">:</span> <span class="token string">"https://asset-packagist.org"</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在你的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-configurations.md" target="_blank" rel="noopener noreferrer">配置数组</a> 中设置 <code v-pre>@npm</code> 和 <code v-pre>@bower</code> 的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-aliases.md" target="_blank" rel="noopener noreferrer">别名</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$config</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line">    <span class="token string single-quoted-string">'aliases'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'@bower'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'@vendor/bower-asset'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'@npm'</span>   <span class="token operator">=></span> <span class="token string single-quoted-string">'@vendor/npm-asset'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以访问 <a href="https://asset-packagist.org/" target="_blank" rel="noopener noreferrer">asset-packagist.org</a> 来了解它是如何工作的。</p>
<h4 id="使用-fxp-composer-asset-plugin" tabindex="-1"><a class="header-anchor" href="#使用-fxp-composer-asset-plugin"><span>使用 fxp/composer-asset-plugin</span></a></h4>
<p>与 asset-packagist 相比，composer-asset-plugin 不需要对应用程序配置进行任何更改。 而是需要运行以下命令来全局安装一个特殊的 Composer 插件：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> global require <span class="token string">"fxp/composer-asset-plugin:^1.4.1"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这个命令会全局安装 <a href="https://github.com/fxpio/composer-asset-plugin" target="_blank" rel="noopener noreferrer">composer asset plugin</a> 插件， 以便使用 Composer 来管理对 Bower 和 NPM 包的依赖。 在这个插件安装后， 你计算机上的每个项目都可以通过 <code v-pre>composer.json</code> 来管理 Bower 和 NPM 包。</p>
<p>如果你想要通过 Yii 来发布它们，将以下行添加到项目的 <code v-pre>composer.json</code> 文件中， 来调整 Bower 和 NPM 安装包的放置目录：</p>
<div class="language-javascript line-numbers-mode" data-highlighter="prismjs" data-ext="js" data-title="js"><pre v-pre class="language-javascript"><code><span class="line"><span class="token string-property property">"config"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token string-property property">"asset-installer-paths"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token string-property property">"npm-asset-library"</span><span class="token operator">:</span> <span class="token string">"vendor/npm"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string-property property">"bower-asset-library"</span><span class="token operator">:</span> <span class="token string">"vendor/bower"</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: 与 asset-packagist 相比，使用 <code v-pre>fxp/composer-asset-plugin</code> 的方式， 会显著减慢 <code v-pre>composer update</code> 命令的速度。</p>
</blockquote>
<p>在配置好 Composer 支持使用 Bower 和 NPM 之后：</p>
<ol>
<li>编辑项目或扩展中的 <code v-pre>composer.json</code> 文件，在该文件的 <code v-pre>require</code> 字段中列举出相关包。 你应该使用 <code v-pre>bower-asset/PackageName</code>（对于 Bower 包）或者 <code v-pre>npm-asset/PackageName</code> （对于 NPM 包）的方式来引入这些包。</li>
<li>运行 <code v-pre>composer update</code></li>
<li>创建资源类并列出在应用程序或扩展中所需使用的 JS 或 CSS 文件。你还需要配置 [[yii\web\AssetBundle::sourcePath]] 属性为 <code v-pre>@bower/PackageName</code> 或 <code v-pre>@npm/PackageName</code>。这是因为 Composer 会将 Bower 或 NPM 软件包安装在与此别名对应的目录中。</li>
</ol>
<blockquote>
<p>Note: 某些包可能会将其所有发布文件放在子目录中。在这种情况下，你应该把子目录设为 [[yii\web\AssetBundle::sourcePath]] 的值。例如，在 <code v-pre>yii\web\JqueryAsset</code> 这个资源包中，使用 <code v-pre>@bower/jquery/dist</code> 而不是 <code v-pre>@bower/jquery</code>。</p>
</blockquote>
<h2 id="使用资源包" tabindex="-1"><a class="header-anchor" href="#使用资源包"><span>使用资源包</span></a></h2>
<p>为使用资源包，先在视图中调用 [[yii\web\AssetBundle::register()]] 方法注册资源，例如，在视图模板可使用如下代码注册资源包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">app<span class="token punctuation">\</span>assets<span class="token punctuation">\</span>AppAsset</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">AppAsset</span><span class="token operator">::</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// $this 代表视图对象</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Info: [[yii\web\AssetBundle::register()]] 方法返回资源包对象，该对象包含了发布资源的信息比如 [[yii\web\AssetBundle::basePath]] 或 [[yii\web\AssetBundle::baseUrl]]。</p>
</blockquote>
<p>如果在其他地方注册资源包，应提供视图对象，如在 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-widgets.md" target="_blank" rel="noopener noreferrer">小部件</a> 类中注册资源包，可以通过 <code v-pre>$this-&gt;view</code> 获取视图对象。</p>
<p>当在视图中注册一个资源包时，在背后 Yii 会注册它所依赖的资源包，如果资源包是放在 Web 不可访问的目录下，会被发布到可访问的目录，后续当视图渲染页面时，会生成这些注册包包含的 CSS 和 JS 文件对应的 <code v-pre>&lt;link&gt;</code> 和 <code v-pre>&lt;script&gt;</code> 标签，这些标签的先后顺序取决于资源包的依赖关系以及在 [[yii\web\AssetBundle::css]] 和 [[yii\web\AssetBundle::js]] 的列出来的前后顺序。</p>
<h3 id="动态资源包" tabindex="-1"><a class="header-anchor" href="#动态资源包"><span>动态资源包</span></a></h3>
<p>作为常规 PHP 类，资源包可以承担一些与之相关的额外逻辑，并可以动态调整其内部参数。比如：你也许用到某些复杂的 JS 库，它提供一些用于国际化的包，并分成单独的源文件：每个文件对应于国际化中所支持的语言。为了让这个 JS 库正常工作，你需要把这些单独的语言源文件加载到页面中。 这可以通过覆盖 [[yii\web\AssetBundle::init()]] 方法来实现：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">namespace</span> <span class="token package">app<span class="token punctuation">\</span>assets</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>AssetBundle</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Yii</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">SophisticatedAssetBundle</span> <span class="token keyword">extends</span> <span class="token class-name">AssetBundle</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$sourcePath</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'/path/to/sophisticated/src'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$js</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'sophisticated.js'</span> <span class="token comment">// file, which is always used</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword static-context">parent</span><span class="token operator">::</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">js</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'i18n/'</span> <span class="token operator">.</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">language</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'.js'</span><span class="token punctuation">;</span> <span class="token comment">// 添加的动态文件</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>特定的资源包也可以通过 [[yii\web\AssetBundle::register()]] 返回的实例进行调整。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">app<span class="token punctuation">\</span>assets<span class="token punctuation">\</span>SophisticatedAssetBundle</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Yii</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$bundle</span> <span class="token operator">=</span> <span class="token class-name static-context">SophisticatedAssetBundle</span><span class="token operator">::</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">view</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$bundle</span><span class="token operator">-></span><span class="token property">js</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'i18n/'</span> <span class="token operator">.</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">language</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'.js'</span><span class="token punctuation">;</span> <span class="token comment">// dynamic file added</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: 虽然支持资源包的动态调整，但这是一种<strong>不好</strong>的做法，可能导致意想不到的副作用，应尽可能避免。</p>
</blockquote>
<h3 id="自定义资源包" tabindex="-1"><a class="header-anchor" href="#自定义资源包"><span>自定义资源包</span></a></h3>
<p>Yii 通过配置名为 <code v-pre>assetManager</code> 的应用组件来使用 <code v-pre>yii\web\AssetManager</code>， 通过配置 [[yii\web\AssetManager::bundles]] 属性，可以自定义资源包的行为，例如 <code v-pre>yii\web\JqueryAsset</code> 资源包默认从 jquery Bower 包中使用 <code v-pre>jquery.js</code> 文件，为了提高可用性和性能，你可能需要从 CDN 服务器上获取 jquery 文件，可以在应用配置中配置 <code v-pre>assetManager</code>，如下所示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'assetManager'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'bundles'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'yii\web\JqueryAsset'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'sourcePath'</span> <span class="token operator">=></span> <span class="token constant">null</span><span class="token punctuation">,</span>   <span class="token comment">// 一定不要发布该资源</span></span>
<span class="line">                    <span class="token string single-quoted-string">'js'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                        <span class="token string single-quoted-string">'//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token punctuation">]</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可通过类似 [[yii\web\AssetManager::bundles]] 配置多个资源包，数组的键应为资源包的类名（最开头不要反斜杠），数组的值为对应的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-configurations.md" target="_blank" rel="noopener noreferrer">配置数组</a>.</p>
<blockquote>
<p>Tip: 可以根据条件判断使用哪个资源，如下示例为如何在开发环境用 <code v-pre>jquery.js</code>，否则用<code v-pre>jquery.min.js</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"> <span class="token string single-quoted-string">'yii\web\JqueryAsset'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">     <span class="token string single-quoted-string">'js'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">         <span class="token constant">YII_ENV_DEV</span> <span class="token operator">?</span> <span class="token string single-quoted-string">'jquery.js'</span> <span class="token punctuation">:</span> <span class="token string single-quoted-string">'jquery.min.js'</span></span>
<span class="line">     <span class="token punctuation">]</span></span>
<span class="line"> <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></blockquote>
<p>可以将资源包名称设置为 <code v-pre>false</code> 来禁用一个或多个资源包，当视图中注册一个禁用资源包，视图不会包含任何该包的资源以及不会注册它所依赖的包，例如，为禁用 <code v-pre>yii\web\JqueryAsset</code>，可以使用如下配置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'assetManager'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'bundles'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'yii\web\JqueryAsset'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可以设置 [[yii\web\AssetManager::bundles]] 为 <code v-pre>false</code> 来禁用 <em>所有</em> 的资源包。</p>
<p>需要记住，使用 [[yii\web\AssetManager::bundles]] 进行自定义时，只在创建资源包时起作用，例如，在对象的构造函数阶段。这意味着在此之后对该资源包对象所做的任何调整都将覆盖在配置数组中 [[yii\web\AssetManager::bundles]] 的配置。特别是：在 [[yii\web\AssetBundle::init()]] 方法内或在注册的资源包对象上进行的调整优先于 <code v-pre>AssetManager</code> 配置。下面给个例子来说明在配置数组中对 [[yii\web\AssetManager::bundles]] 的配置是不起作用的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// Program source code:</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">app<span class="token punctuation">\</span>assets</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>AssetBundle</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Yii</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">LanguageAssetBundle</span> <span class="token keyword">extends</span> <span class="token class-name">AssetBundle</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword static-context">parent</span><span class="token operator">::</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">baseUrl</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'@web/i18n/'</span> <span class="token operator">.</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">language</span><span class="token punctuation">;</span> <span class="token comment">// 将不能通过 `AssetManager` 来管理！</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"><span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$bundle</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>app<span class="token punctuation">\</span>assets<span class="token punctuation">\</span>LargeFileAssetBundle</span><span class="token operator">::</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">view</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$bundle</span><span class="token operator">-></span><span class="token property">baseUrl</span> <span class="token operator">=</span> <span class="token constant">YII_DEBUG</span> <span class="token operator">?</span> <span class="token string single-quoted-string">'@web/large-files'</span><span class="token punctuation">:</span> <span class="token string single-quoted-string">'@web/large-files/minified'</span><span class="token punctuation">;</span> <span class="token comment">// 将不能通过 `AssetManager` 来管理！</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Application config :</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'assetManager'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'bundles'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'app\assets\LanguageAssetBundle'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'baseUrl'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://some.cdn.com/files/i18n/en'</span> <span class="token comment">// 这行代码不起作用！</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'app\assets\LargeFileAssetBundle'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'baseUrl'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://some.cdn.com/files/large-files'</span> <span class="token comment">// 这行代码不起作用！</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="资源映射" tabindex="-1"><a class="header-anchor" href="#资源映射"><span>资源映射</span></a></h3>
<p>有时你想“修复”多个资源包中资源文件的错误或者不兼容，例如包 A 使用 1.11.1 版本的 <code v-pre>jquery.min.js</code>，包 B 使用 2.1.1 版本的 <code v-pre>jquery.js</code>，可自定义每个包来解决这个问题，但更好的方式是使用 <em>资源映射</em> 的特性来把不正确的资源部署为想要的，为此，配置 [[yii\web\AssetManager::assetMap]] 属性，如下所示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'assetManager'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'assetMap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'jquery.js'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>[[yii\web\AssetManager::assetMap]] 的键为你想要修复的资源名，值为你想要使用的资源路径，当视图注册资源包，在 [[yii\web\AssetBundle::css]] 和 [[yii\web\AssetBundle::js]] 数组中每个相关资源文件会和该部署进行对比，如果数组任何键对比为资源文件的最后文件名（如果有的话前缀为 [[yii\web\AssetBundle::sourcePath]]），对应的值为替换原来的资源。例如，资源文件 <code v-pre>my/path/to/jquery.js</code> 匹配键 <code v-pre>jquery.js</code>.</p>
<blockquote>
<p>Note: 只有相对路径指定的资源对应到资源部署，替换的资源路径可以为绝对路径，也可为和 [[yii\web\AssetManager::basePath]] 相关的路径。</p>
</blockquote>
<h3 id="资源发布" tabindex="-1"><a class="header-anchor" href="#资源发布"><span>资源发布</span></a></h3>
<p>如前所述，如果资源包放在 Web 不能访问的目录，当视图注册资源时资源会被拷贝到一个 Web 可访问的目录中，这个过程称为<em>资源发布</em>，[[yii\web\AssetManager]] 会自动处理该过程。</p>
<p>资源默认会发布到 <code v-pre>@webroot/assets</code> 目录，对应的 URL 为 <code v-pre>@web/assets</code>，可配置 [[yii\web\AssetManager::basePath]] 和 [[yii\web\AssetManager::baseUrl]] 属性自定义发布位置。</p>
<p>除了拷贝文件方式发布资源，如果操作系统和 Web 服务器允许可以使用符号链接，该功能可以通过设置 [[yii\web\AssetManager::linkAssets]] 为 <code v-pre>true</code> 来启用。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'assetManager'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'linkAssets'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>使用以上配置，资源管理器会创建一个符号链接到要发布的资源包源路径，这比拷贝文件方式快并能确保发布的资源一直为最新的。</p>
<h3 id="清除缓存" tabindex="-1"><a class="header-anchor" href="#清除缓存"><span>清除缓存</span></a></h3>
<p>对于运行在生产模式的 Web 应用程序，通常会为资源包和其他静态资源开启 HTTP 缓存。但这种做法有个不好的地方就是，当你更新某个资源并部署到生产环境时，客户端可能由于 HTTP 缓存而仍然使用旧版本的资源。为了克服该不足，你可以试试清除缓存特性，它由 2.0.3 版本引入，只需如下配置 <code v-pre>yii\web\AssetManager</code> 即可：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'assetManager'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'appendTimestamp'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>通过上述配置后，每个已发布资源的 URL 都会附加一个最后更新时间戳的信息。比如，<code v-pre>yii.js</code> 的 URL 可能是 <code v-pre>/assets/5515a87c/yii.js?v=1423448645&quot;</code>，这里的参数 v 表示 <code v-pre>yii.js</code> 文件的最后更新时间戳。现在一旦你更新了某个资源，它的 URL 也会改变进而强制客户端获取该资源的最新版本。</p>
<h2 id="常用资源包" tabindex="-1"><a class="header-anchor" href="#常用资源包"><span>常用资源包</span></a></h2>
<p>Yii框架定义许多资源包，如下资源包是最常用，可在你的应用或扩展代码中引用它们。</p>
<ul>
<li>[[yii\web\YiiAsset]]：主要包含 <code v-pre>yii.js</code> 文件，该文件完成模块 JS 代码组织功能，也为 <code v-pre>data-method</code> 和 <code v-pre>data-confirm</code> 属性提供特别支持和其他有用的功能。有关 <code v-pre>yii.js</code> 的更多信息可以在 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/output-client-scripts.md#yii.js" target="_blank" rel="noopener noreferrer">客户端脚本部分</a> 中找到。</li>
<li>[[yii\web\JqueryAsset]]：包含 jQuery Bower 包的 <code v-pre>jquery.js</code> 文件。</li>
<li>[[yii\bootstrap\BootstrapAsset]]：包含 Twitter Bootstrap 框架的 CSS 文件。</li>
<li>[[yii\bootstrap\BootstrapPluginAsset]]：包含 Twitter Bootstrap 框架的 JS 文件来支持 Bootstrap JS 插件。</li>
<li>[[yii\jui\JuiAsset]]：包含 jQuery UI 库的 CSS 和 JS 文件。</li>
</ul>
<p>如果你的代码需要 jQuery，jQuery UI 或 Bootstrap，应尽量使用这些预定义资源包而非自己创建，如果这些包的默认配置不能满足你的需求，可以自定义配置，详情参考 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-assets.md#customizing-asset-bundles" target="_blank" rel="noopener noreferrer">自定义资源包</a>。</p>
<h2 id="资源转换" tabindex="-1"><a class="header-anchor" href="#资源转换"><span>资源转换</span></a></h2>
<p>除了直接编写 CSS 或 JS 代码，开发人员经常使用扩展语法来编写，再使用特殊的工具将它们转换成 CSS 或 JS。例如，对于 CSS 代码可使用 <a href="http://lesscss.org/" target="_blank" rel="noopener noreferrer">LESS</a> 或 <a href="http://sass-lang.com/" target="_blank" rel="noopener noreferrer">SCSS</a>，对于 JS 可使用 <a href="http://www.typescriptlang.org/" target="_blank" rel="noopener noreferrer">TypeScript</a>。</p>
<p>可将使用扩展语法的资源文件列到资源包的 [[yii\web\AssetBundle::css]] 和 [[yii\web\AssetBundle::js]] 中，如下所示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">AppAsset</span> <span class="token keyword">extends</span> <span class="token class-name">AssetBundle</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$basePath</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'@webroot'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$baseUrl</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'@web'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$css</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'css/site.less'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$js</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'js/site.ts'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$depends</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'yii\web\YiiAsset'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'yii\bootstrap\BootstrapAsset'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当在视图中注册一个这样的资源包，[[yii\web\AssetManager]] 资源管理器会自动运行预处理工具将使用扩展语法 的资源转换成 CSS/JS，当视图最终渲染页面时，在页面中包含的是 CSS/JS 文件， 而不是原始的扩展语法代码文件。</p>
<p>Yii 使用文件扩展名来表示资源使用哪种扩展语法，默认可以识别如下语法和文件扩展名：</p>
<ul>
<li><a href="https://lesscss.org/" target="_blank" rel="noopener noreferrer">LESS</a>：<code v-pre>.less</code></li>
<li><a href="https://sass-lang.com/" target="_blank" rel="noopener noreferrer">SCSS</a>：<code v-pre>.scss</code></li>
<li><a href="https://stylus-lang.com/" target="_blank" rel="noopener noreferrer">Stylus</a>：<code v-pre>.styl</code></li>
<li><a href="https://coffeescript.org/" target="_blank" rel="noopener noreferrer">CoffeeScript</a>：<code v-pre>.coffee</code></li>
<li><a href="https://www.typescriptlang.org/" target="_blank" rel="noopener noreferrer">TypeScript</a>：<code v-pre>.ts</code></li>
</ul>
<p>Yii 依靠安装的预处理工具来转换资源，例如，为使用 <a href="https://lesscss.org/" target="_blank" rel="noopener noreferrer">LESS</a>，应安装 <code v-pre>lessc</code> 预处理命令。</p>
<p>可配置 [[yii\web\AssetManager::converter]] 自定义预处理命令和支持的扩展语法，如下所示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'assetManager'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'converter'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\web\AssetConverter'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'commands'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'less'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'css'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'lessc {from} {to} --no-color'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'ts'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'js'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'tsc --out {to} {from}'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如上所示，通过 [[yii\web\AssetConverter::commands]] 属性指定支持的扩展语法，数组的键为文件扩展名（前面不需要这样的一个小点：<code v-pre>.</code>），数组的值为目标资源文件扩展名和执行资源转换的命令，命令中的标记 <code v-pre>{from}</code> 和 <code v-pre>{to}</code> 会分别被源资源文件路径和目标资源文件路径替代。</p>
<blockquote>
<p>Info: 除了以上方式，也有其他的方式来处理扩展语法资源，例如，可使用编译工具如 <a href="http://gruntjs.com/" target="_blank" rel="noopener noreferrer">grunt</a> 来监控并自动转换扩展语法资源，此时，应使用资源包中编译后的 CSS/JS 文件而不是原始文件。</p>
</blockquote>
<h2 id="合并和压缩资源" tabindex="-1"><a class="header-anchor" href="#合并和压缩资源"><span>合并和压缩资源</span></a></h2>
<p>一个 Web 页面可以包含很多 CSS 和 JS 文件，为减少 HTTP 请求和这些下载文件的大小，通常的方式是在页面中合并并压缩多个 CSS/JS 文件为一个或很少的几个文件，并使用压缩后的文件而不是原始文件。</p>
<blockquote>
<p>Info: 合并和压缩资源通常应用在产品上线模式，在开发模式下使用原始的 CSS/JS 更方便调试。</p>
</blockquote>
<p>接下来介绍一种合并和压缩资源文件 而不需要修改已有代码的方式：</p>
<ol>
<li>找出应用中所有你想要合并和压缩的资源包，</li>
<li>将这些包分成一个或几个组，注意每个包只能属于其中一个组，</li>
<li>合并/压缩每个组里 CSS 文件到一个文件，同样方式处理 JS 文件，</li>
<li>为每个组定义新的资源包：
<ul>
<li>设置 [[yii\web\AssetBundle::css]] 和 [[yii\web\AssetBundle::js]] 属性分别为压缩后的 CSS 和 JS 文件；</li>
<li>自定义设置每个组内的资源包，设置资源包的 [[yii\web\AssetBundle::css]] 和 [[yii\web\AssetBundle::js]] 属性为空, 并设置它们的 [[yii\web\AssetBundle::depends]] 属性为每个组新创建的资源包。</li>
</ul>
</li>
</ol>
<p>使用这种方式，当在视图中注册资源包时，会自动触发原始包所属的组资源包的注册，然后，页面就会包含以合并或压缩的资源文件，而不是原始文件。</p>
<h3 id="示例" tabindex="-1"><a class="header-anchor" href="#示例"><span>示例</span></a></h3>
<p>使用一个示例来解释以上这种方式：</p>
<p>假定你的应用有两个页面 X 和 Y，页面 X 使用资源包 A，B 和 C，页面 Y 使用资源包 B，C 和 D。</p>
<p>有两种方式划分这些资源包，一种使用一个组包含所有资源包，另一种是将（A，B，C）放在组 X，（B，C，D）放在组 Y，哪种方式更好？第一种方式优点是两个页面使用相同的已合并 CSS 和 JS 文件，从而使 HTTP 缓存更高效，另一方面，由于单个组包含所有文件，已合并的 CSS 和 JS 文件会更大，因此会增加文件传输时间，在这个示例中，我们使用第一种方式，也就是用一个组包含所有包。</p>
<blockquote>
<p>Info: 将资源包分组并不是无意义的，通常要求分析现实中不同页面各种资源的数据量，开始时为简便使用一个组。</p>
</blockquote>
<p>在所有包中使用工具(例如 <a href="https://developers.google.com/closure/compiler/" target="_blank" rel="noopener noreferrer">Closure Compiler</a>，<a href="https://github.com/yui/yuicompressor/" target="_blank" rel="noopener noreferrer">YUI Compressor</a>) 来合并和压缩 CSS 和 JS 文件，注意合并后的文件满足包间的先后依赖关系，例如，如果包 A 依赖 B，B 依赖 C 和 D，那么资源文件列表以 C 和 D 开始，然后为 B 最后为 A。</p>
<p>合并和压缩之后，会得到一个 CSS 文件和一个 JS 文件，假定它们的名称为 <code v-pre>all-xyz.css</code> 和 <code v-pre>all-xyz.js</code>，<code v-pre>xyz</code> 为使文件名唯一以避免HTTP缓存问题的时间戳或哈希值。</p>
<p>现在到最后一步了，在应用配置中配置 <code v-pre>yii\web\AssetManager</code> 资源管理器如下所示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'assetManager'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'bundles'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'all'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\web\AssetBundle'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'basePath'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'@webroot/assets'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'baseUrl'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'@web/assets'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'css'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'all-xyz.css'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'js'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'all-xyz.js'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'A'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'css'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'js'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'depends'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'all'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'B'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'css'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'js'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'depends'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'all'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'C'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'css'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'js'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'depends'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'all'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'D'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'css'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'js'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'depends'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'all'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-assets.md#customizing-asset-bundles" target="_blank" rel="noopener noreferrer">自定义资源包</a> 小节中所述，如上配置改变每个包的默认行为，特别是包 A、B、C 和 D 不再包含任何资源文件，都依赖包含合并后的 <code v-pre>all-xyz.css</code> 和 <code v-pre>all-xyz.js</code> 文件的包 <code v-pre>all</code>，因此，对于页面X会包含这两个合并后的文件而不是包 A、B、C 的原始文件，对于页面 Y 也是如此。</p>
<p>最后有个方法更好地处理上述方式，除了直接修改应用配置文件，可将自定义包数组放到一个文件，在应用配置中根据条件包含该文件，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'assetManager'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'bundles'</span> <span class="token operator">=></span> <span class="token keyword">require</span> <span class="token constant">__DIR__</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'/'</span> <span class="token operator">.</span> <span class="token punctuation">(</span><span class="token constant">YII_ENV_PROD</span> <span class="token operator">?</span> <span class="token string single-quoted-string">'assets-prod.php'</span> <span class="token punctuation">:</span> <span class="token string single-quoted-string">'assets-dev.php'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如上所示，在产品上线模式下资源包数组存储在 <code v-pre>assets-prod.php</code> 文件中，不是产品上线模式存储在 <code v-pre>assets-dev.php</code> 文件中。</p>
<blockquote>
<p>Note: 这种资源合并的机制是基于 [[yii\web\AssetManager::bundles]] 能够覆盖已经注册的资源包。但是，正如前面提到的，并不能覆盖到使用 [[yii\web\AssetBundle::init()]] 方法或在资源包对象上进行调整的资源包。你应该避免在资源合并时使用此类动态捆绑的资源包。</p>
</blockquote>
<h3 id="使用-asset-命令" tabindex="-1"><a class="header-anchor" href="#使用-asset-命令"><span>使用 <code v-pre>asset</code> 命令</span></a></h3>
<p>Yii 提供一个名为 <code v-pre>asset</code> 控制台命令来使上述操作自动处理。</p>
<p>为使用该命令，应先创建一个配置文件设置哪些资源包要合并以及分组方式， 可使用 <code v-pre>asset/template</code> 子命令来生成一个模板， 然后修改成你想要的模板。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii asset/template assets.php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>该命令在当前目录下生成一个名为 <code v-pre>assets.php</code> 的文件，文件的内容类似如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 为控制台命令"yii asset"使用的配置文件</span>
<span class="line"> * 注意在控制台环境下，一些路径别名如 '@webroot' 和 '@web' 不会存在</span>
<span class="line"> * 请定义不存在的路径别名</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// 为JavaScript文件压缩修改 command/callback </span></span>
<span class="line">    <span class="token string single-quoted-string">'jsCompressor'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'java -jar compiler.jar --js {from} --js_output_file {to}'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token comment">// 为CSS文件压缩修改command/callback </span></span>
<span class="line">    <span class="token string single-quoted-string">'cssCompressor'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'java -jar yuicompressor.jar --type css {from} -o {to}'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token comment">// 是否在压缩后删除资源来源：</span></span>
<span class="line">    <span class="token string single-quoted-string">'deleteSource'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token comment">// 要压缩的资源包列表</span></span>
<span class="line">    <span class="token string single-quoted-string">'bundles'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// 'yii\web\YiiAsset',</span></span>
<span class="line">        <span class="token comment">// 'yii\web\JqueryAsset',</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token comment">// 资源包压缩后的输出</span></span>
<span class="line">    <span class="token string single-quoted-string">'targets'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'all'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\web\AssetBundle'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'basePath'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'@webroot/assets'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'baseUrl'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'@web/assets'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'js'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'js/all-{hash}.js'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'css'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'css/all-{hash}.css'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token comment">// 资源管理器配置:</span></span>
<span class="line">    <span class="token string single-quoted-string">'assetManager'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>应修改该文件的 <code v-pre>bundles</code> 的选项指定哪些包你想要合并，在 <code v-pre>targets</code> 选项中应指定这些包如何分组，如前述的可以指定一个或多个组。</p>
<blockquote>
<p>Note: 由于在控制台应用别名 <code v-pre>@webroot</code> 和 <code v-pre>@web</code> 不可用，应在配置中明确指定它们。</p>
</blockquote>
<p>JS 文件会被合并压缩后写入到 <code v-pre>js/all-{hash}.js</code> 文件，其中 {hash} 会被结果文件的哈希值替换。</p>
<p><code v-pre>jsCompressor</code> 和 <code v-pre>cssCompressor</code> 选项指定控制台命令或 PHP 回调函数来执行 JS 和 CSS 合并和压缩，Yii 默认使用 <a href="https://developers.google.com/closure/compiler/" target="_blank" rel="noopener noreferrer">Closure Compiler</a> 来合并 JS 文件，使用 <a href="https://github.com/yui/yuicompressor/" target="_blank" rel="noopener noreferrer">YUI Compressor</a> 来合并 CSS 文件，你应手工安装这些工具或修改选项使用你喜欢的工具。</p>
<p>根据配置文件，可执行 <code v-pre>asset</code> 命令来合并和压缩资源文件并生成一个新的资源包配置文件 <code v-pre>assets-prod.php</code>：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii asset assets.php config/assets-prod.php</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>生成的配置文件可以在应用配置中包含，如最后一小节所描述的。</p>
<blockquote>
<p>Note: 如果你使用 [[yii\web\AssetManager::bundles]] 或 [[yii\web\AssetManager::assetMap]] 来自定义应用程序的资源包，并希望将此自定义应用于压缩的源文件中，你应该在 asset 命令配置文件中的 <code v-pre>assetManager</code> 部分包含这些自定义的内容。</p>
</blockquote>
<blockquote>
<p>Note: 在指定压缩源时，应避免使用那些根据参数动态调整的资源包（即在 <code v-pre>init()</code> 方法或注册后根据参数进行动态调整的包），因为在压缩后它们可能无法正常工作。</p>
</blockquote>
<blockquote>
<p>Info: 使用 <code v-pre>asset</code> 命令并不是合并和压缩资源的唯一方法。你也可以使用能够自动运行设定任务的项目构建工具 <a href="http://gruntjs.com/" target="_blank" rel="noopener noreferrer">grunt</a> 来实现同样的目的。</p>
</blockquote>
<h3 id="资源包分组" tabindex="-1"><a class="header-anchor" href="#资源包分组"><span>资源包分组</span></a></h3>
<p>上一小节，介绍了如何压缩所有的资源包到一个文件，减少对应用中引用资源文件的 HTTP 请求数，但是在实践中很少这样做。比如，应用有一个“前端”和一个“后端”，每一个都用了一个不同 JS 和 CSS 文件集合。在这种情况下，把所有的资源包压缩到一个文件毫无意义，“前端”不会用到“后端”的资源文件，当请求“前端”页面时，“后端”的资源文件也会被发送过来，浪费网络带宽。</p>
<p>为了解决这个问题，可以吧资源包分成若干组，每个组里面有若干个资源包。下面的配置展示了如何对资源包分组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line">    <span class="token comment">// Specify output bundles with groups:</span></span>
<span class="line">    <span class="token string single-quoted-string">'targets'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'allShared'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'js'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'js/all-shared-{hash}.js'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'css'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'css/all-shared-{hash}.css'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'depends'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token comment">// 包含由'backend' 和 'frontend' 共享的资源包</span></span>
<span class="line">                <span class="token string single-quoted-string">'yii\web\YiiAsset'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'app\assets\SharedAsset'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'allBackEnd'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'js'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'js/all-{hash}.js'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'css'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'css/all-{hash}.css'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'depends'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token comment">// 只包含 'backend' 资源:</span></span>
<span class="line">                <span class="token string single-quoted-string">'app\assets\AdminAsset'</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'allFrontEnd'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'js'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'js/all-{hash}.js'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'css'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'css/all-{hash}.css'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'depends'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment">// 包含所有的剩余资源</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如上所示，资源包分成了三个组：<code v-pre>allShared</code>，<code v-pre>allBackEnd</code> 和 <code v-pre>allFrontEnd</code> 它们每个都依赖各自的资源包集合。 比如，<code v-pre>allBackEnd</code> 依赖 <code v-pre>app\assets\AdminAsset</code>。当对该配置运行 <code v-pre>asset</code> 命令时，将会根据各自依赖合并资源包。</p>
<blockquote>
<p>Info: 你也可以把某个分组的 <code v-pre>depends</code> 配置留空。这样做得话，这个分组将会依赖剩余的资源包，剩余资源包是指不被其他分组依赖的那些资源包。</p>
</blockquote>
<blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


